package org.jiuling.servlet.tools.request;

import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import org.jiuling.basics.validation.StringVerify;
import org.jiuling.servlet.tools.enums.IpHeaderEnum;


/**
 * 解析请求
 * @author 紫灵
 * @date 2021年11月30日 15时19分50秒
 */
public class AnalysisRequest {

    /**
     * 私有构造函数，禁止创建对象
     */
    private AnalysisRequest(){}

    private static final String UNKNOWN = "unknown";

    /**
     * 获取IP
     * @param request 请求
     * @return IP
     */
    public static String getIp(HttpServletRequest request){
        String ip = request.getHeader(IpHeaderEnum.X_FORWARDED_FOR.getName());
        if (ip != null && ip.length() != 0 && !UNKNOWN.equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if(ip.contains(",")){
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取IP
     * @param request 请求
     * @return IP
     */
    public static String getIp(ServletRequest request){
        return getIp((HttpServletRequest) request);
    }

    /**
     * 获取系统
     * @param request 请求
     * @return 系统
     */
    public static String getOs(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        if (StringVerify.isBlank(userAgent)) return null;
        // 系统
        if (userAgent.contains(OsEnum.WINDOWS.getOs())) return OsEnum.WINDOWS.getOs();
        if (userAgent.contains(OsEnum.MAC.getOs())) return OsEnum.MAC.getOs();
        if (userAgent.contains(OsEnum.UNIX.getOs())) return OsEnum.UNIX.getOs();
        if (userAgent.contains(OsEnum.ANDROID.getOs())) return OsEnum.ANDROID.getOs();
        if (userAgent.contains(OsEnum.IPHONE.getOs())) return OsEnum.IPHONE.getOs();
        if (userAgent.contains(OsEnum.UBUNTU.getOs())) return OsEnum.UBUNTU.getOs();
        if (userAgent.contains(OsEnum.LINUX.getOs())) return OsEnum.LINUX.getOs();
        return OsEnum.UNKNOWN.getOs();
    }

    /**
     * 获取系统
     * @param request 请求
     * @return 系统
     */
    public static String getOs(ServletRequest request){
        return getOs((HttpServletRequest) request);
    }

    /**
     * 获取浏览器
     * @param request 请求
     * @return 浏览器
     */
    public static String getBrowser(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        // 浏览器名称数组
        String[] browserNameArray = {"Firefox","Chrome"};
        String browserInfo;
        // 进行通用浏览器遍历
        for (String browserName : browserNameArray) {
            browserInfo = getGeneralBrowserInfo(userAgent,browserName);
            if (!StringVerify.isBlank(browserInfo)) return browserInfo;
        }
        browserInfo = "Unknown";
        if (userAgent.contains("Safari")) return "Safari";
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) return "Internet Explorer";
        return browserInfo;
    }

    /**
     * 获取通用浏览器信息
     * @param userAgent 用户代理
     * @param browserName 浏览器名称
     * @return 浏览器
     */
    private static String getGeneralBrowserInfo(String userAgent,String browserName){
        if (!userAgent.contains(browserName)) return null;
        int start = userAgent.indexOf(browserName+"/");
        if (start == -1) return null;
        int end = userAgent.indexOf(' ', start);
        if (end == -1) {
            end = userAgent.length();
        }
        return userAgent.substring(start, end);
    }

    /**
     * 获取浏览器
     * @param request 请求
     * @return 浏览器
     */
    public static String getBrowser(ServletRequest request){
        return getBrowser((HttpServletRequest) request);
    }

    /**
     * 系统枚举
     */
    private enum OsEnum{
        WINDOWS("Windows"),
        UBUNTU("Ubuntu"),
        LINUX("Linux"),
        MAC("Mac"),
        UNIX("Unix"),
        ANDROID("Android"),
        IPHONE("Iphone"),
        UNKNOWN("UnKnown");
        private String os;
        OsEnum(String os){
            this.os = os;
        }
        public String getOs() {
            return os;
        }
    }
}
